package com.litelimit.core.strategy;

import com.litelimit.core.limit.annotation.LiteLimit;
import com.litelimit.core.service.log.LiteLimitLog;
import com.litelimit.core.strategy.core.AbstractLimitStrategy;
import com.litelimit.core.strategy.core.LimitMethod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 策略管理器
 */
public class LimitStrategyManager {

    private final Map<LimitMethod, AbstractLimitStrategy> strategyMap = new HashMap<>();

    public LimitStrategyManager(List<AbstractLimitStrategy> strategies){
        strategies.forEach(
                strategy -> strategyMap.put(strategy.getLimitMethod(), strategy)
        );
        LiteLimitLog.log.debug("策略初始化成功：" + strategies);
    }

    public boolean tryAcquire(LiteLimit limit) {
        if (limit.strategy().equals(LimitMethod.CUSTOM)){
            // TODO
        }
        return strategyMap.get(limit.strategy()).tryAcquire(limit);
    }
}
